package com.naiterui.ehp.bs.doctor.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.naiterui.ehp.bp.domain.Doctor;
import com.naiterui.ehp.bp.domain.DrPatientRemarkname;
import com.naiterui.ehp.bp.domain.Patient;
import com.naiterui.ehp.bp.domain.PatientReservation;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bs.doctor.dao.IDoctorDao;
import com.naiterui.ehp.bs.doctor.dao.IDrPatientRemarkDao;
import com.naiterui.ehp.bs.doctor.dao.IPatientDao;
import com.naiterui.ehp.bs.doctor.repository.IPatientReservationRepository;
import com.naiterui.ehp.bs.doctor.service.IPatientReservationService;
import com.naiterui.ehp.bs.doctor.service.IPersonalMsgService;
import com.naiterui.ehp.bs.doctor.utils.PatientUtil;
import com.naiterui.ehp.bs.doctor.utils.SMSUtil;

/**
 * 视频预约相关
 */
@Service
public class AdPatientReservationServiceImpl implements IPatientReservationService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdPatientReservationServiceImpl.class);
    @Autowired
    private IDoctorDao doctorDao;
    @Autowired
    private IPatientDao patientDao;
    @Autowired
    private IPatientReservationRepository patientReservationDao;
    @Autowired
    private IDrPatientRemarkDao iDrPatientRemarknameDao;
    @Autowired
    private IPersonalMsgService iPersonalMsgService;


    @Override
    public void remind(Long reservationId) throws BusinessException {

        LOGGER.info("系统开始提醒医生到达预约时间");

        PatientReservation patientReservation = this.patientReservationDao.get(reservationId);
        if (patientReservation == null || patientReservation.getStatus() != PatientReservation.STATUS_NOT_CONSULTING) {
            LOGGER.error("没有查询到预约订单，预约ID：{}", reservationId);
            return;
        }

        Patient patient = this.patientDao.get(patientReservation.getPatientId());
        Doctor doctor = this.doctorDao.get(patientReservation.getDoctorId());
        if (patient == null || doctor == null) {
            LOGGER.error("没有查询到患者，患者ID{}", patientReservation.getId());
            return;
        }

        DrPatientRemarkname remarkname = this.iDrPatientRemarknameDao.getRemarkname(patientReservation.getDoctorId(),
                                                                                    patientReservation.getPatientId());
        String patientName = this.getPatientName(patient, remarkname);
        String pushStr = String.format(ConfigUtil.getString("reservation.sms.remind.template"), doctor.getName(), patientName);
        LOGGER.info("给医生发送PUSH,医生ID:{} 内容:{}", doctor.getId(), pushStr);

        this.iPersonalMsgService.sendVideoPushMessage(doctor.getId(), pushStr);

        SMSUtil.asyncSendSMS(doctor.getPhone(), SMSUtil.Template.DR_REMIND, doctor.getName(), patientName);

    }


    /**
     * 获取患者名称
     * PatientReservationServiceImpl.getPatientName()
     *
     * @param patient    患者基本信息
     * @param remarkname 备注名等信息
     *
     * @return
     *
     * @Author wangzehao
     * @Date 2016年8月8日
     * @since 1.0.0
     */
    private String getPatientName(Patient patient, DrPatientRemarkname remarkname) {
        // 备注名>姓名>昵称。
        String patientName = PatientUtil.getPatientName(patient, remarkname);
        if (StringUtils.isEmpty(patientName)) {
            patientName = "互联网医院患者";
        }
        return patientName;
    }


}
